<template>
  <div id="app">
    <nav class="nav">
      <div class="nav-container">
        <h1>演示系统</h1>
      </div>
    </nav>
    <div class="login-wrapper">
      <div class="login-panel">
        <div class="ribbon-right"></div>
        <div class="ribbon-left"></div>
        <div class="login-box">
          <div class="head-portrait">
            <img
              src="@/assets/login/head-portrait.png"
              onerror="this.onerror=null;this.src='/static/resource/img/index/head-portrait.png'"
            />
          </div>
          <div class="input-group m-b">
            <span class="input-group-addon">
              <img src="@/assets/login/user.png" />
            </span>
            <input
              type="text"
              placeholder="用户名"
              class="form-control"
              v-model.trim="account"
            />
          </div>
          <div class="input-group m-b">
            <span class="input-group-addon">
              <img src="@/assets/login/password.png" />
            </span>
            <input
              type="password"
              placeholder="密码"
              class="form-control"
              v-model.trim="password"
              @keyup.enter="loginIn"
            />
          </div>
          <div class="input-group m-b">
              <div class="btn btn-block login-btn" @click="loginIn">登录</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { Message } from 'element-ui'
import request from '@/utils/request'
import { setUserInfo } from '@/utils/storage'

export default {
  name: 'LoginIndex',
  mounted () {
    console.log(process.env)
  },
  data () {
    return {
      account: '',
      password: ''
    }
  },
  methods: {
    // 登录方法
    async loginIn () {
      if (!this.account || !this.password) {
        Message.error({
          message: '请输入账号/密码',
          duration: 800
        })
        return
      }
      const data = {
        account: this.account,
        password: this.password
      }
      const res = await request.post('/login/loginIn', data)
      // 正确则成功登录并且需要设置用户的token
      if (res.code === 200) {
        await setUserInfo({
          token: res.data.token,
          userId: res.data.uid
        })
        this.$router.push('/main')
      }
    }
  }
}
</script>
<style scoped lang="less">
.nav {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 60px;
  min-width: 1200px;
  background-color: rgba(14, 29, 52, 0.6);

  .nav-container {
    width: 1200px;
    margin: 0 auto;
    height: 60px;

    h1 {
      margin: 0;
      padding: 10px 15px;
      font-weight: normal;
      font-size: 18px;
      color: white;
      line-height: 40px;
    }
  }
}

.login-wrapper {
  position: absolute;
  z-index: -1;
  top: 0;
  left: 0;
  min-width: 1200px;
  height: 100%;
  min-height: 800px;
  width: 100%;
  background-position: center;
  background: url("@/assets/login/500445148.jpg") no-repeat;
  background-size: cover;

  .login-panel {
    position: absolute;
    top: 0;
    right: 300px;
    width: 420px;

    .ribbon-left {
      position: absolute;
      top: 0;
      left: 0;
      height: 285px;
      width: 252px;
      z-index: 1;
      background-image: url("@/assets/login/ribbon-top.png");
    }

    .ribbon-right {
      position: absolute;
      top: 0;
      right: 0;
      height: 284px;
      width: 252px;
      background-image: url("@/assets/login/ribbon-bottom.png");
    }

    .login-box {
      position: absolute;
      top: 260px;
      right: 20px;
      width: 380px;
      height: 430px;
      background-color: #fff;
      border-radius: 15px;
      box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);
      padding: 0 40px 30px 40px;

      .head-portrait {
        margin-top: 70px;
        margin-bottom: 30px;
        text-align: center;

        img {
          width: 66px;
          height: 66px;
          border-radius: 50%;
        }
      }

      .input-group {
        display: flex;
        margin-bottom: 20px;

        .input-group-addon {
          height: 40px;
          width: 40px;
          padding: 0;
          text-align: center;
          background-color: #ddd;
          display: flex;
          align-items: center;
          justify-content: center;
          img {
            width: 22px;
            height: 22px;
          }
        }

        input {
          flex: 1;
          padding: 6px 12px;
          height: 40px;
          border: 1px solid #ddd;
          background-color: #FFFFFF;
          background-image: none;
          border-radius: 1px;
          color: inherit;
          transition: border-color 0.15s ease-in-out 0s, box-shadow 0.15s ease-in-out 0s;
          font-size: 14px;
        }

        .login-btn {
          margin-top: 30px;
          height: 40px;
          line-height: 40px;
          background-color: #3958a5;
          text-align: center;
          flex: 1;
          color: #fff;
          cursor: pointer;
          user-select: none;

          &:hover {
            background-color: #2e4a90;
          }
        }
      }
    }
  }
}
</style>
